//
//  CustomView.m
//  绘制图片
//
//  Created by 伍银荣 on 2021/7/2.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "CustomView.h"

@implementation CustomView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
   // [self drawImageWithRect:rect];
    [self customImageViewWithRect:rect];
}



//在UIView上显示一个剪裁后的图片
- (void)customImageViewWithRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"热巴"];
    //1.获取UIView的图形上下文对象
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.在图形上下文上绘制一个圆形
    //CGContextAddRect(ctx, CGRectMake(0, 0, 300, 300));
    CGContextAddArc(ctx, 175, 175, 150, 0, M_PI * 2, 1);
   // CGContextFillPath(ctx);
    //3.执行剪裁操作(剪裁的意思是告诉系统,将来只有在被裁剪出的区域内绘制的图形才会显示)
    CGContextClip(ctx);
    //4.把图片绘制到上下文上(直接调用UIImage对象的绘图方法即可)
    [image drawInRect:rect];
}

//绘制图片
- (void)drawImageWithRect:(CGRect)rect {
   //首先绘制大图
    UIImage *image = [UIImage imageNamed:@"花"];
    //从某一个点开始绘制
   // [image drawAtPoint:CGPointZero];
    //绘制到某一个区域(拉伸,会将一张大图缩小在真个视图中)
    [image drawInRect:rect];
    //平铺到某一个区域
   // [image drawAsPatternInRect:rect];
    
    
    //绘制小图
   // UIImage *image = [UIImage imageNamed:@"VIP"];
    //从某一个点开始绘制
   // [image drawAtPoint:CGPointMake(50, 50)];
    //绘制到某一个区域(拉伸,会将一张小图拉伸到整个视图中)
   // [image drawInRect:rect];
    //平铺
   // [image drawAsPatternInRect:rect];
}

@end
